#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QListWidget>
#include <QFileIconProvider>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //IconType
    QListWidget *pListWidget = ui->listWidget;
    pListWidget->setIconSize(QSize(48, 48));         // 设置单元项图片大小
    pListWidget->setResizeMode(QListView::Adjust);   // 设置大小模式-可调节
    pListWidget->setViewMode(QListView::IconMode);   // 设置显示模式
    pListWidget->setMovement(QListView::Static);     // 设置单元项不可被拖动
    pListWidget->setSpacing(10);                     // 设置单元项间距
    for (int i = 0; i < 7; ++i)
    {
        // 获取图标
        QFileIconProvider provider;
        QIcon icon = provider.icon((QFileIconProvider::IconType)i);

        // 添加单元项
        QListWidgetItem *pItem = new QListWidgetItem(pListWidget);
        pItem->setIcon(icon);
        pListWidget->addItem(pItem);
    }

    //QFileInfo
    QStringList strList;
    strList << QString::fromLocal8Bit("新建文件夹") << QString::fromLocal8Bit("新建公文包")
            << QString::fromUtf8("不存在此文件.exe") << QString::fromLocal8Bit("新建 360压缩 ZIP 文件.zip")
            << QString::fromLocal8Bit("新建 HTTP Archive File.har") << QString::fromLocal8Bit("新建 HttpWatch Log File.hwl")
            << QString::fromLocal8Bit("新建 Microsoft Access 数据库.accdb") << QString::fromLocal8Bit("新建 Microsoft Excel 工作表.xlsx")
            << QString::fromLocal8Bit("新建 Microsoft PowerPoint 演示文稿.pptx") << QString::fromLocal8Bit("新建 Microsoft Word 文档.docx")
            << QString::fromLocal8Bit("新建日记本文档.jnt") << QString::fromLocal8Bit("新建文本文档.txt");

    for (int i = 0; i < strList.count(); ++i)
    {
        QFileInfo info;
        info.setFile(QString("E:\\%1").arg(strList.at(i)));

        // 获取图标、类型
        QFileIconProvider provider;
        QIcon icon = provider.icon(info);
        QString strType = provider.type(info);

        // 添加单元项
        QListWidgetItem *pItem = new QListWidgetItem(pListWidget);
        pItem->setIcon(icon);
        pItem->setText(strType);
        pListWidget->addItem(pItem);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
